<?php
include_once 'config.php';
$_SESSION['Response'] = 0;
if ($_POST != NULL) {
	if (isset($_POST['Login'])) {
		$Json = json_decode($_POST['Login']);
		if (isset($Json -> UserPhone) && $Json -> UserPhone != NULL && $Json -> UserPassword != NULL) {
			$_SESSION['state'] = LoginByPhone($Json -> UserPhone, $Json -> UserPassword);
		} else if ($Json -> UserEmail != NULL && $Json -> UserPassword != NULL) {
			$_SESSION['state'] = LoginByEmail($Json -> UserEmail, $Json -> UserPassword);
		} else {
			$_SESSION['state'] = FALSE;
		}
	} else if (isset($_POST['Register'])) {
		$Json = json_decode($_POST['Register']);
		if ($Json -> UserPhone != NULL && $Json -> UserPassword != NULL && $Json -> UserEmail != NULL) {
			if (!(IsPhoneExist($Json -> UserPhone))) {
				if (!(IsEmailExist($Json -> UserEmail))) {
					Register($Json -> UserPhone, $Json -> UserPassword, $Json -> UserName, $Json -> UserGender, $Json -> UserEmail);
				} else {
					$_SESSION['Response'] = 102;
					//邮箱已经存在
				}
			} else {
				$_SESSION['Response'] = 101;
				//手机号已经存在
			}
		} else if ($Json -> UserPhone != NULL && $Json -> UserPassword != NULL && $Json -> UserEmail == NULL) {
			if (!(IsPhoneExist($Json -> UserPhone))) {
				Register($Json -> UserPhone, $Json -> UserPassword, $Json -> UserName, $Json -> UserGender);
			} else {
				$_SESSION['Response'] = 101;
			}
		} else {
			$_SESSION['Response'] = 103;
		}
	} else if (isset($_POST['Action'])) {
		if ($_POST['Action'] == 'LogOut') {
			LogOut();
		}
	}
}
if (isset($_SESSION['Response'])) {
	echo $_SESSION['Response'];
} else {
	echo 0;
}
function LoginById($UserId, $UserPassword) {
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from users where user_id='" . $UserId . "' and user_password='" . $UserPassword . "'";
	$Result = $Link_mysql -> query($SqlSelect);
	if ($Result -> num_rows > 0) {
		$UserInfo = mysqli_fetch_array($Result, MYSQLI_ASSOC);
		//		$_SESSION['user'] = json_encode($UserInfo);
		$_SESSION['user'] = $UserInfo;
		$_SESSION['Response'] = 100;
		return TRUR;
	} else {
		$_SESSION['user'] = NULL;
		$_SESSION['Response'] = 104;
		//登录失败
		return FALSE;
	}
	mysqli_close($Link_mysql);
}

function LoginByEmail($UserEmail, $UserPassword) {
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from users where user_email='" . $UserEmail . "' and user_password='" . $UserPassword . "'";
	$Result = $Link_mysql -> query($SqlSelect);
	if ($Result -> num_rows > 0) {
		$UserInfo = mysqli_fetch_array($Result, MYSQLI_ASSOC);
		//		$_SESSION['user'] = json_encode($UserInfo);
		$_SESSION['user'] = $UserInfo;
		$_SESSION['Response'] = 100;
		return TRUE;
	} else {
		$_SESSION['user'] = NULL;
		$_SESSION['Response'] = 104;
		return FALSE;
	}
	mysqli_close($Link_mysql);
}

function LoginByPhone($UserPhone, $UserPassword) {
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from users where user_phone='" . $UserPhone . "' and user_password='" . $UserPassword . "'";
	$Result = $Link_mysql -> query($SqlSelect);
	if ($Result -> num_rows > 0) {
		$UserInfo = mysqli_fetch_array($Result, MYSQLI_ASSOC);
		//		$_SESSION['user'] = json_encode($UserInfo);
		$_SESSION['user'] = $UserInfo;
		$_SESSION['Response'] = 100;
		return TRUE;
	} else {
		$_SESSION['user'] = NULL;
		$_SESSION['Response'] = 104;
		return FALSE;
	}
	mysqli_close($Link_mysql);
}

function Register($UserPhone, $UserPassword, $UserName, $UserGender, $UserEmail) {
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlInsert = '';
	if (!(isset($UserEmail)) || $UserEmail == NULL || $UserEmail == '') {
		$SqlInsert = "insert into users(user_phone,user_password,user_name,user_gender,user_email) values('" . $UserPhone . "','" . $UserPassword . "','" . $UserName . "'," . $UserGender . ")";
	} else {
		$SqlInsert = "insert into users(user_phone,user_password,user_name,user_gender,user_email) values('" . $UserPhone . "','" . $UserPassword . "','" . $UserName . "'," . $UserGender . ",'" . $UserEmail . "')";
	}
//	echo $SqlInsert . "            ";
	if ($Link_mysql -> query($SqlInsert)) {
		$_SESSION['state'] = LoginByPhone($UserPhone, $UserPassword);
	} else {
		$_SESSION['Response'] = 105;
		echo mysqli_error($Link_mysql);
		session_unset();
	}
	mysqli_close($Link_mysql);
}

function IsPhoneExist($UserPhone) {
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from users where user_phone = '" . $UserPhone . "'";
	$Result = $Link_mysql -> query($SqlSelect);
	if ($Result -> num_rows > 0) {
		mysqli_close($Link_mysql);
		return TRUE;
	} else {
		mysqli_close($Link_mysql);
		return FALSE;
	}
}

function IsEmailExist($UserEmail) {
	$Link_mysql = mysqli_connect(Server, User, Pwd, Database);
	$SqlSelect = "select * from users where user_email = '" . $UserEmail . "'";
	$Result = $Link_mysql -> query($SqlSelect);
	if ($Result -> num_rows > 0) {
		mysqli_close($Link_mysql);
		return TRUE;
	} else {
		mysqli_close($Link_mysql);
		return FALSE;
	}
}

function LogOut() {
	unset($_SESSION);
	session_unset();

	$_SESSION['state'] = FALSE;
	$_SESSION['Response'] = 0;
}
?>